function init_poreceived() {
	var dwrCallMetaData = {
		callback : success_findpobycompany,
		exceptionHandler : exception_generic_handler,
		timeout : 100000,
		errorHandler : exception_generic_handler
	};

	DWR_PurchaseOrders.fetchPurchaseOrdersForCompany(usrcompany_uid,false,true,
			dwrCallMetaData);
}

var customAttachmentFormatter = function(elLiner, oRecord, oColumn, oData) {
    if(oRecord.getData("orderAttachments") != null &&  typeof oRecord.getData("orderAttachments") != 'undefined' ) {
    	var attachment = oRecord.getData("orderAttachments")[0];
        elLiner.innerHTML = "<a href="+attachment.uniqueName+">"+ 'Download'+ "</a>";
    }
    else {
    	  elLiner.innerHTML = "N/A";
    }
};

var poreceived_cache = null;
function success_findpobycompany(dwrresp) {
	por_yui_table_instance = create_poresults_results_table(por_yui_table_instance,"poreceived_yui_table",porcolumnDefs);
	clear_poresults_results_table(por_yui_table_instance) ;
	por_yui_table_instance.render();
	
	if (dwrresp && dwrresp.respCode == 0) {
		if (dwrresp.objRespOrError != null && dwrresp.objRespOrError["PO_RECD"] != null && dwrresp.objRespOrError["PO_RECD"].length > 0) {
			//orderAttachments
			poreceived_cache = dwrresp.objRespOrError["PO_RECD"];
			por_yui_table_instance.addRows(dwrresp.objRespOrError["PO_RECD"]);
			por_yui_table_instance.render();
			
			//set the sort column
			var col = por_yui_table_instance.getColumn('orderNumber');
			//sort records here
			por_yui_table_instance.sortColumn(col,YAHOO.widget.DataTable.CLASS_DESC );
		}
		$("po_rece_throbber").className = "throbberOff";
	}
}

var porcolumnDefs = [ {
	key : "orderNumber",
	label : "Order#",
	formatter : "hlinkFormatter",
	sortable : true,
	sortOptions:{sortFunction:sortByPONum}
}, {
	key : "clientNameText",
	label : "Buyer",
	sortable : true
}, {
	key : "orderDate",
	label : "Date",
	formatter: "date",
	sortable : true
} , {
	key : "orderAmount",
	label : "Amount",
	formatter: "currency"
} , {
	key : "versionNumber",
	label : "Ver"
}];

var por_yui_table_instance = null;
function create_poresults_results_table(instanceid, divid,coldefs) {
	if(instanceid == null){


	var porDataSource = new YAHOO.util.DataSource();
	porDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
	 var oConfigs = {
             paginator: new YAHOO.widget.Paginator({
                 rowsPerPage: 2
                 
             }),
             initialRequest: "results=504",
             width: "600px",
             // we'll leave the actual loading for later
 			initialLoad:true, 
 			sortedBy:{key:"orderNumber", dir:"desc"},
 				
 			// This function links the master to the nested tables
 			generateNestedRequest: function (record) {
 				return lineItemData;

 			}
     };
	 /*por_yui_table_instance = new YAHOO.widget.NestedDataTable(
				'poreceived_yui_table', 
				porcolumnDefs,
				porDataSource,
				lineItemReadOnlycolumnDefs,
				lineItemDataSource,
				oConfigs);*/
	 instanceid = new YAHOO.widget.DataTable(divid,coldefs,porDataSource,oConfigs);
	 instanceid.set("selectionMode", "single");
	 instanceid.subscribe("rowClickEvent",
			 instanceid.onEventSelectRow);
	 instanceid.subscribe("radioClickEvent", function(oArgs){
		 var elCheckbox = oArgs.target;
         var oRecord = this.getRecord(elRadio);
         poxml_to_open = oRecord._oData.orderXml;
       	  
     });
	//por_yui_table_instance.subscribe("nestedToggleEvent",additemsr_tonested); 
	}
	return instanceid;
}

function clear_poresults_results_table(instanceid) {
	if (instanceid != null) {
		var length = instanceid.getRecordSet().getLength();
		if (length > 0) {
			instanceid.deleteRows(0, length);
		}
	}
}

function additemsr_tonested(oArgs){
	var parentid = oArgs.td.parentNode.id;
	if(poreceived_cache != null && poreceived_cache.length > 0 && por_yui_table_instance != null){
		var rsln = poreceived_cache.length;
		for(var i=0;i<rsln;i++){
				var rec = por_yui_table_instance.getRecordSet().getRecord(parentid);
				var onum = rec.getData("orderNumber");
				
				if(onum == poreceived_cache[i].orderNumber && poreceived_cache[i].orderLineItems != null && poreceived_cache[i].orderLineItems.length > 0 && !(poreceived_cache[i].lineitems_added == "ADDED")){
					rec._oData.__NESTED__.dt.addRows(poreceived_cache[i].orderLineItems);
					rec._oData.__NESTED__.dt.render();
					poreceived_cache[i].lineitems_added = "ADDED";
					break;
				}
			}
	}
}